#include "controlview.h"
#include "logger.h"
#include <QLabel>
#include <QFormLayout>
#include <QHBoxLayout>

using namespace views;
using namespace utilities;

ControlView::ControlView()
{
    initUI();
    setupUI();

    // Setup necessary event listeners
    connect(_btnTakeReference, SIGNAL(clicked()), this, SLOT(emitTakeReference()));
    connect(_btnRecordVideo, SIGNAL(clicked()), this, SLOT(emitRecordVideo()));
    connect(_btnTakeSnapshot, SIGNAL(clicked()), this, SLOT(emitTakeSnapshot()));
    connect(_btnRecognizeObject, SIGNAL(clicked()), this, SLOT(emitRecognizeObject()));
}

ControlView::~ControlView()
{
    delete _mainFBox;
    delete _btnTakeReference;
    delete _btnRecordVideo;
    delete _btnTakeSnapshot;
    delete _btnRecognizeObject;
}

void ControlView::initUI()
{
    Logger::logMessage("ControlView::initUI()");

    _mainFBox           = new QFrame;
    _btnTakeReference   = new QPushButton(QString::fromUtf8("Prendre"));
    _btnRecordVideo     = new QPushButton(QString::fromUtf8("Enregistrer"));
    _btnTakeSnapshot    = new QPushButton(QString::fromUtf8("Prendre"));
    _btnRecognizeObject = new QPushButton(QString::fromUtf8("Commencer"));
}

void ControlView::setupUI()
{
    Logger::logMessage("ControlView::setupUI()");

    // Setup the main Grid Box
    //this->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 255, 255);"));
    this->setMaximumHeight(125);

    // Setup widgets
    _btnTakeReference->setMaximumWidth(100); // Add img to record button in sha Allah

    // Setup the layout and add widgets
    QGridLayout *layout = new QGridLayout;
    layout->setSpacing(0);

    // Title
    QLabel *title0   = new QLabel(QString::fromUtf8("Contrôles"));
    title0->setStyleSheet(QString::fromUtf8("font: 12pt \"Rockwell\"; font-weight: bold; color: rgb(0, 0, 0);"));
    layout->addWidget(title0, 0, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);

    // Reference image
    QLabel *title   = new QLabel(QString::fromUtf8("Image de référence"));
    title->setStyleSheet(QString::fromUtf8("font: 8pt \"Rockwell\"; font-weight: bold; color: rgb(0, 0, 0);"));
    title->setMinimumWidth(110);
    layout->addWidget(title, 1, 0, 1, 1, Qt::AlignLeft);
    layout->addWidget(_btnTakeReference, 1, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);

    QLabel *title1   = new QLabel(QString::fromUtf8("Reconnaissance objet"));
    title1->setStyleSheet(QString::fromUtf8("font: 8pt \"Rockwell\"; font-weight: bold; color: rgb(0, 0, 0);"));
    title1->setMinimumWidth(110);
    layout->addWidget(title1, 1, 2, 1, 1, Qt::AlignLeft);
    layout->addWidget(_btnRecognizeObject, 1, 3, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    layout->addWidget(new QLabel, 1, 4, 1, 5, Qt::AlignLeft | Qt::AlignTop);

    // Video recording
    QLabel *title2  = new QLabel(QString::fromUtf8("Vidéo"));
    title2->setMinimumWidth(110);
    title2->setStyleSheet(QString::fromUtf8("font: 8pt \"Rockwell\"; font-weight: bold; color: rgb(0, 0, 0);"));
    layout->addWidget(title2, 2, 0, 1, 1, Qt::AlignLeft);
    layout->addWidget(_btnRecordVideo, 2, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    layout->addWidget(new QLabel, 2, 2, 1, 7, Qt::AlignLeft | Qt::AlignTop);

    // Snapshot saving
    QLabel *title3  = new QLabel(QString::fromUtf8("Snapshot"));
    title3->setMinimumWidth(110);
    title3->setStyleSheet(QString::fromUtf8("font: 8pt \"Rockwell\"; font-weight: bold; color: rgb(0, 0, 0);"));
    layout->addWidget(title3, 3, 0, 1, 1, Qt::AlignLeft);
    layout->addWidget(_btnTakeSnapshot, 3, 1, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    layout->addWidget(new QLabel, 3, 2, 1, 7, Qt::AlignLeft | Qt::AlignTop);

    // Set the layout
    _mainFBox->setFrameStyle(QFrame::Box);
    QPalette* palette = new QPalette();
    palette->setColor(QPalette::Foreground,Qt::green);
    palette->setColor(QPalette::Background,Qt::white);
    _mainFBox->setPalette(*palette);
    _mainFBox->setLayout(layout);

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(_mainFBox);
    this->setLayout(mainLayout);
}

void ControlView::setViewName(const QString& name)
{
    Logger::logMessage("ControlView::setViewName(const QString& name)");
    //_viewName = name;
}

void ControlView::emitTakeReference()
{
    emit takeReference();
}

void ControlView::emitRecordVideo()
{
    emit recordVideo();
}

void ControlView::emitTakeSnapshot()
{
    emit takeSnapshot();
}

void ControlView::emitRecognizeObject()
{
    emit recognizeObject();
}
